package com.xncoding.CountDownLatch;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {
    private static CountDownLatch countDownLatch = new CountDownLatch(5);

    // Boss线程，等待员工到齐开会
    static class BossThread extends Thread {
        @Override
        public void run() {
            System.out.println("Boss进入会议室准备材料...");
            System.out.println("Boss在会议室等待...");
            try {
                countDownLatch.await(); // Boss等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("Boss等到人齐了，开始开会...");
        }
    }

    // 员工到达会议室
    static class EmpleoyeeThread extends Thread {
        @Override
        public void run() {
            System.out.println("员工" + Thread.currentThread().getName()
                    + "，到达会议室....");
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) {
        // Boss线程启动
        new BossThread().start();

        // 员工到达会议室
        for (int i = 0; i < countDownLatch.getCount(); i++) {
            new EmpleoyeeThread().start();
        }
    }
}
